package com.github.phoenics.newlives.common.exception;

import com.github.phoenics.newlives.common.base.RestResponse;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 基础的异常
 *
 * @author Phoenics Chow
 * on 2019年09月24日
 */
@Slf4j
public class CommonApiException extends RuntimeException {
    private String errorKey;
    private List<String> args = Lists.newArrayList();
    public CommonApiException(String errorKey, String... args) {
        this.errorKey = errorKey;
        if (args != null) {
            this.args.addAll(Arrays.asList(args));
        }
    }

    public String getErrorKey() {
        return this.errorKey;
    }

    public List<String> getArgs() {
        return Collections.unmodifiableList(args);
    }
    @Override
    public String getMessage() {
        RestResponse restResponse = ApiExceptionUtils.getApiExceptionByErrorKey(this.getErrorKey(), this.getArgs());
        if(restResponse!=null){
            return restResponse.getMessage();
        }else {
            log.error("errorkey [{}] can't find value", this.getErrorKey());
        }
        return "";
    }

    public static String getMessage(String errorKey, String... args){
        return new CommonApiException(errorKey,args).getMessage();
    }


}
